﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Happy.ExtentionMethods;
using Happy.Dto;
using Happy.Domain.CommonModel.TreeLike;

namespace Happy.Query
{
    /// <summary>
    /// 扩展了<see cref="IQuerySource{T}"/>。
    /// </summary>
    public static class IQuerySourceExtentions
    {
        /// <summary>
        /// 返回所有记录。
        /// </summary>
        public static IEnumerable<T> All<T>(this IQuerySource<T> that)
        {
            return that.Fetch(QueryArgument.All);
        }

        /// <summary>
        /// 返回满足条件的记录集合。
        /// </summary>
        public static IEnumerable<T> Fetch<T>(this IQuerySource<T> that, object condition)
        {
            condition.MustNotNull("condition");

            var query = QueryArgument.Create(condition);

            return that.Fetch(query);
        }

        ///// <summary>
        ///// 返回指定的孩子记录。
        ///// </summary>
        //public static IEnumerable<T> Children<T>(this IQuerySource<T> that, Guid? id)
        //    where T : ITreeNodeView<Guid>
        //{
        //    return that.Children<T, Guid>(id);
        //}

        ///// <summary>
        ///// 返回指定的孩子记录。
        ///// </summary>
        //public static IEnumerable<T> Children<T, TId>(this IQuerySource<T> that, TId? id)
        //    where T : ITreeNodeView<TId>
        //    where TId : struct
        //{
        //    var query = QueryArgument.Create(new { ParentId = id });

        //    return that.Fetch(query);
        //}

        ///// <summary>
        ///// 返回指定的孩子记录。
        ///// </summary>
        //public static IEnumerable<T> Children<T>(this IQuerySource<T> that, QueryArgument query, Guid? id)
        //    where T : ITreeNodeView<Guid>
        //{
        //    return that.Children<T, Guid>(query, id);
        //}

        ///// <summary>
        ///// 返回指定的孩子记录。
        ///// </summary>
        //public static IEnumerable<T> Children<T, TId>(this IQuerySource<T> that, QueryArgument query, TId? id)
        //    where T : ITreeNodeView<TId>
        //    where TId : struct
        //{
        //    query.WhereClauses.Add(WhereClause.Create(new { ParentId = id }));

        //    return that.Fetch(query);
        //}

        ///// <summary>
        ///// 返回指定路径下的所有记录。
        ///// </summary>
        //public static IEnumerable<T> Descendents<T, TId>(this IQuerySource<T> that, string path, bool includeSelf)
        //    where T : ITreeNodeView<TId>
        //    where TId : struct
        //{
        //    var searchPath = includeSelf ? (path + "%") : (path + TreeNodeConstant.NodePathSeparator + "%");

        //    var query = QueryArgument.Create("NodePath LIKE @0", new object[] { searchPath });

        //    return that.Fetch(query);
        //}
    }
}
